{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "try:\n",
    "    import ruamel_yaml as yaml\n",
    "except ImportError:\n",
    "    from ruamel import yaml\n",
    "from email.utils import formatdate\n",
    "BlockMap = yaml.comments.CommentedMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def FlowMap(*args, **kwargs):\n",
    "    m = yaml.comments.CommentedMap(*args, **kwargs)\n",
    "    m.fa.set_flow_style()\n",
    "    return m\n",
    "\n",
    "def FlowList(*args, **kwargs):\n",
    "    lst = yaml.comments.CommentedSeq(*args, **kwargs)\n",
    "    lst.fa.set_flow_style()\n",
    "    return lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "emitter = yaml.YAML()\n",
    "emitter.width = 70\n",
    "out_name='test.yaml'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "TChemSimulation = {'title':'Zero D ignition simulation',\\\n",
    "                  'date':formatdate(localtime=True)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "FileNames = {'chem':'chem.inp', 'thermo':'term.dat'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "TimeIntegration = {'tbegin':0,'tend':1,\\\n",
    "                   'dtmin':1e-10,'dtmax':1e-2,\\\n",
    "                   'absolute_tolerance_newton':1e-16,'relative_tolerance_newton':1e-8,\\\n",
    "                   'relative_tolerance_time':1e-6,'absolute_tolerance_time':1e-12,\\\n",
    "                   'max_newton_iterations':20, 'max_time_iterations':100\n",
    "                   }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "OutputFile = {'file_name':'IgnSolution.dat', 'output_frequency':10}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "IgnitionDelayTime = {'threshold_temperature':1500,'compute_only_ignition_delay_time':'false',\\\n",
    "                     'file_name_second_derivative':'IgnitionDelayTime.dat',\\\n",
    "                     'file_name_Tthreshold':\"IgnitionDelayTimeTthreshold.dat\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "SimulationSettings = {'verbose':'false','team_size':-1,'vector_size':-1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "Header = ['T','P','H2','O2']\n",
    "sample = np.array([[900, 101325, 0.12, 0.88],[800, 101325, 0.2, 0.8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "DesignOfExperiment = {'variables':FlowList(Header)}\n",
    "Nsamples = 2\n",
    "for i in range(Nsamples):\n",
    "    sampleNo = sample[i,:].tolist()\n",
    "    DesignOfExperiment.update({'condition_No_'+str(i):FlowList(sampleNo)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "ReactionIndex = [1, 2, 3 ]\n",
    "Nreaction = len(ReactionIndex)\n",
    "μ=0\n",
    "σ=0.25\n",
    "A = np.random.lognormal(μ, σ, size=(Nsamples, Nreaction))\n",
    "β = np.ones((Nsamples, Nreaction))\n",
    "E = np.random.lognormal(μ, σ, size=(Nsamples, Nreaction))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "DesignOfExperiment.update({'reaction_index':FlowList(ReactionIndex)})\n",
    "for i in range(Nsamples):\n",
    "    DesignOfExperiment.update({'modifier_pre_exporencial_No_'+str(i):FlowList( A[i,:].tolist() )})\n",
    "    DesignOfExperiment.update({'modifier_temperature_coefficient_No_'+str(i):FlowList( β[i,:].tolist() )})\n",
    "    DesignOfExperiment.update({'modifier_activation_energy_No_'+str(i):FlowList( E[i,:].tolist() )})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(out_name, 'w') as dest:\n",
    "    emitter.dump({'TChem Simulation':TChemSimulation}, dest)\n",
    "    emitter.dump({'FileNames':FileNames}, dest)\n",
    "    emitter.dump({'TimeIntegration':TimeIntegration}, dest)\n",
    "    emitter.dump({'OutputFile':OutputFile}, dest)\n",
    "    emitter.dump({'IgnitionDelayTime':IgnitionDelayTime}, dest)\n",
    "    emitter.dump({'SimulationSettings':SimulationSettings}, dest)\n",
    "    emitter.dump({'DesignOfExperiment':DesignOfExperiment}, dest)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
